package com.mao.ui;

import com.mao.bean.employee;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Date;

public class EmployeeManagerUI extends JFrame{
    private JFrame frame;
    private JTable table; //表格
    private DefaultTableModel model; //表格模型：封装表格数据的对象
    private JTextField nametextFieldSearch;
    // 准备一个静态集合对象，用于存储系统中的全部员工对象信息。
    private static ArrayList<employee> employees = new ArrayList<>();

    public EmployeeManagerUI() {
    }

    public EmployeeManagerUI(String username) {
        super("欢迎， "+username+"进入人事信息管理界面");
        frame = this;
        initialize();
        this.setVisible(true); //显示窗体
    }

    private void initialize() {
        this.setBounds(100, 100, 800, 600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().setLayout(new BorderLayout());

        // 输入框和搜索按钮
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        nametextFieldSearch  = new JTextField(20);
        JButton searchButton = new JButton("搜索");
        JButton addButton = new JButton("添加");
        topPanel.add(nametextFieldSearch);
        topPanel.add(searchButton);
        topPanel.add(addButton);

        // 创建表格模型
        model = new DefaultTableModel(
                new Object[][]{},
                new String[]{"ID", "姓名", "性别", "年龄","电话","职位","入职时间","薪水", "部门信息"}
        ){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setRowHeight(30);

        // 添加数据到表格
        for(int i = 0; i < 2; i++)
        {
            model.addRow(new Object[]{i+1, "张三" + (i+1), i % 2 == 0 ? "男" : "女", 21, "12345678901", "经理", new Date().toLocaleString(), 5000, "部门" +i+1});
        }

        // 右键菜单
        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem editItem = new JMenuItem("编辑");
        JMenuItem deleteItem = new JMenuItem("删除");
        popupMenu.add(editItem);
        popupMenu.add(deleteItem);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON3){
                    int row = table.rowAtPoint(e.getPoint());
                    if(row>=0){
                        table.setRowSelectionInterval(row, row);
                        popupMenu.show(table, e.getX(), e.getY());
                    }
                }
            }
        });


        // 绑定事件到菜单项
        editItem.addActionListener(e -> {
            int selectedRow = table.getSelectedRow();
            if(selectedRow >= 0)
            {
                int id = (Integer)model.getValueAt(selectedRow,0);// 假设ID在第一行
                JOptionPane.showMessageDialog(frame, "编辑 ID：" + id);
            }
        });


        deleteItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = table.getSelectedRow();
                if(selectedRow >= 0)
                {
                    int id = (Integer)model.getValueAt(selectedRow,0);// 假设ID在第一行
                    JOptionPane.showMessageDialog(frame, "删除 ID：" + id);
                }
            }
        });

        // 搜索按钮监听器
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nametextFieldSearch.getText();
            }
        });

        // 添加按钮监听器
        addButton.addActionListener(e -> {
            // 弹出一个添加员工信息的界面出来。
            new AddEmployeeUI(this);
        });

        frame.getContentPane().add(topPanel, BorderLayout.NORTH);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    }

    public void addEmployee(employee employee) {
        // 添加员工信息到集合，展示在表格中
        employees.add(employee);
        // 添加一行员工信息到表格中
        model.addRow(new Object[]{employee.getId(), employee.getName(), employee.getSex(), employee.getAge(), employee.getPhone(), employee.getJob(), employee.getEntryTime(), employee.getSalary(), employee.getDept()});

    }
}
